package model

import (
	"context"
	"github.com/LyricTian/gin-admin/v7/internal/app/contextx"
	"github.com/LyricTian/gin-admin/v7/internal/app/model/gormx/entity"
	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
)

//contentType 1:语音 2:视频
//video id 内容id
func (a *Like) GetLikeNum(ctx context.Context, videoId string, contentType int) (like, favorite int) {
	//  统计点赞收藏

	db := entity.GetLikeDB(ctx, a.DB).Where("video_id = ? and content_type = ? and type = 1", videoId, contentType)
	if db.Count(&like).Error != nil {
		return
	}

	db = entity.GetLikeDB(ctx, a.DB).Where("video_id = ? and content_type = ? and type = 2", videoId, contentType)
	if db.Count(&favorite).Error != nil {
		return
	}
	return
}

//判断当前用户，对该视频资源是否点赞
func (a *Like) IsLike(ctx context.Context, videoId string, contentType int) bool {
	userId, ok := contextx.FromUserID(ctx)
	if !ok {
		return false
	}
	data, err := a.GetFirst(ctx, schema.LikeQueryParam{
		VideoId:     videoId,
		Type:        1,
		ContentType: contentType,
		Creator:     userId,
	})
	if err != nil {
		return false
	}
	if data == nil {
		return false
	}

	return true
}
